package com.gsn.poker.play;

import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.equations.Sine;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.Remove;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.engine.tween.ActorAccessor;
import com.gsn.engine.tween.GsnTween;
import com.gsn.poker.asset.PokerTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.logic.CardGroupLogic;

public class CardGroup extends Group {
	final static int NO_CARD = 5;
	int numCard = 0;
	CardImg[] cards = new CardImg[NO_CARD];
	public boolean data[] = {false, false, false, false, false};
	public int numIsDeal = 0;
	float pad = 5;
	int id;
	Group effGroup = new Group();
	int type;
	public int count = 0;
	public Image arrow;
	public boolean isArrow;
	
	public static CardGroup createTemplate(CardGroupLogic card){
		CardGroup group = new CardGroup();
		for (int i = 0; i < card.getCardsCount(); i++){
			group.nhanBai(card.getCard(i).Id, false);
		}
		return group;
	}
	
	public CardGroup() {
		transform = GsnGame.transform;
		int cpad = 0;
		float tmpPad = 0;
		arrow = new Image(PokerTexture.inst().arrow);
		addActor(arrow);
		arrow.visible = false;
		for (int i = 0; i < NO_CARD; i++){			
			cards[i] = new CardImg(52);
			cpad = (int) (cards[i].width * 0.35f);
			if (i == 0){				
				cards[i].setDefaultPos(0, 0);
			} else {				
				cards[i].setDefaultPos(cards[i - 1].x + cpad, 0);
			}
			addActor(cards[i]);
			cards[i].visible = false;		
		}
		ActorUtility.setRatio(arrow, 0.5f, -1f, cards[4].x + cards[4].width/2, cards[4].height);
		this.width = cards[NO_CARD - 1].x + cards[NO_CARD - 1].width; 
		this.height = cards[NO_CARD - 1].height;	
		
		effGroup.width = this.width;
		effGroup.height = this.height;
		addActor(effGroup);
	}
	
	public void setArrow(boolean isArrow)
	{
		this.isArrow = isArrow;
		
	}
	
	public void newGame(){
		Gdx.app.log("gan cho numisdeal ****    = 0 ", " Mat day ");
		numCard = 0;
		numIsDeal = 0;
		arrow.visible = false;
		for (int i = 0; i < NO_CARD;i++){
			cards[i].visible = false;
			cards[i].setDark(false);
			
			data[i] = false;
			cards[i].xuongBai();
		}
	}	
	
	public void nhanBai(int id, boolean tween){
	//	Gdx.app.log(" NUm cARD ", numCard + " ***********   " + data[numCard]);
		if(data[numCard] == false)
		{
			cards[numCard].setId(id);
			cards[numCard].visible = true;
			
			if (tween && cards[numCard].getId() != 52){
				cards[numCard].color.set(0.5f, 0.5f, 0.5f, 1f);
				Tween.to(cards[numCard], ActorAccessor.TINT, 0.3f).target(1, 1, 1).ease(Sine.IN). start(MyPoker.game.tweenManger);
			}
			
			if(numCard == 4 && cards[numCard].getId() == 52)
			{
				arrow.visible = isArrow;
			}
		}
		numCard++;
		
	}

	public void removeImg(){
		effGroup.clear();
	}
	
	public void showImg(Actor anh, float duration) {
		removeImg();
		
		effGroup.addActor(anh);
		ActorUtility.setRatio(anh, 0.5f, 0, width / 2, - anh.height);
		anh.scaleX = 0.5f;
		anh.scaleY = 1;
		Tween.to(anh, ActorAccessor.SCALE_XY, 0.3f).target(1, 1).ease(Sine.OUT).start(MyPoker.game.tweenManger);
		if (duration >= 0)
			anh.action(Delay.$(Remove.$(), duration));
	}

	public void packCard() {
		int i;
		for (i = 0; i < NO_CARD; i++)
			if (!cards[i].visible)
				break;
		this.width = cards[NO_CARD - 1].width * i;
	}

	public void showResult(Actor anh, float duration) {
		//removeImg();
		addActor(anh);
		ActorUtility.setRatio(anh, 0.5f, 0.5f, width / 2, height / 2);
		if (duration >= 0)
			anh.action(Delay.$(Remove.$(), duration));		
	}
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		// TODO Auto-generated method stub
		if(arrow.visible)
		{
			if(count==5)
			{
				arrow.y = arrow.y + arrow.height/2;
			}
			else if(count == 10)
			{
				arrow.y = arrow.y - arrow.height/2;
				count = 0;
			}
			count++;
		}
		super.draw(batch, parentAlpha);
	}
}
